<template>
  <div class="add">
    <h1>商品管理 -- 添加商品</h1>
    <div class="add-box">
      <el-form
        ref="form"
        :model="form"
        label-width="140px"
        size="mini"
        :rules="rules"
      >
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="form.name" placeholder="商品名称"></el-input>
        </el-form-item>
        <el-form-item label="商品描述" prop="subtitle">
          <el-input v-model="form.subtitle"></el-input>
        </el-form-item>
        <el-form-item label="当前状态" prop="staus">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="在售" :value="1"></el-option>
            <el-option label="已下架" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属分类" prop="fenlei">
          <el-select v-model="form.fenlei" placeholder="请选择分类">
            <el-option
              :label="item.name"
              :value="item.name"
              v-for="item in fenlei"
              :key="item.id"
              @click.native="sect(item.id)"
            ></el-option>
          </el-select>
          <el-select
            v-model="form.jl"
            placeholder="二级分类"
            v-show="flag"
            prop="jl"
          >
            <el-option
              :label="item.name"
              :value="item.name"
              v-for="item in er"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品库存" prop="stock">
          <el-input v-model="form.stock"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input v-model="form.price"></el-input>
        </el-form-item>
        <el-form-item label="商品图片">
          <el-upload
            class="upload-demo"
            action="/api/manage/product/upload.do"
            name="upload_file"
            multiple
            :limit="3"
           :on-success='succ'
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
        <el-form-item label="商品详情" prop="detail">
          <el-input v-model="form.detail"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <p><el-button type="primary" @click="sub">提交</el-button></p>
  </div>
</template>

<script>
import { addlist, er, fenlei } from "@/utils/api";
export default {
  data() {
    return {
      id: 1,
      form: {
        name: "",
        subtitle: "",
        status: 1,
        stock: "",
        price: "",
        detail: "",
        fenlei: "",
        jl: "",
        url:''
      },
      fenlei: [],
      er: [],
      

      flag: false,
      rules: {
        name: [{ required: true, message: "不能为空", trigger: "blur" }],
        subtitle: [{ required: true, message: "不能为空", trigger: "blur" }],
        status: [{ required: true, message: "不能为空", trigger: "blur" }],
        stock: [{ required: true, message: "不能为空", trigger: "blur" }],
        price: [{ required: true, message: "不能为空", trigger: "blur" }],
        fenlei: [{ required: true, message: "不能为空", trigger: "blur" }],
        jl: [{ required: true, message: "不能为空", trigger: "blur" }],
        detail: [{ required: true, message: "不能为空", trigger: "blur" }]
      }
    };
  },
  mounted() {
    fenlei().then(res => {
      this.fenlei = res.data.data;
    });
  },
  methods: {
    succ(a,){
      this.form.url=a.data.uri;
    },
    sect(id) {
      this.flag = true;
      er(id).then(res => {
        this.er = res.data.data;
      });
    },
    sub() {
      let obj = {
        categoryId: this.id++,
        name: this.form.name,
        subtitle: this.form.subtitle,
        status: this.form.status,
        stock: this.form.stock,
        price: this.form.price,
        detail: this.form.detail,
        imgs: this.form.url
      };
      // console.log(obj.imgs);
       addlist(obj).then(res => {
            if (res.status == 0) {
              alert("你添加成功了");
            }
            this.$router.push({ path: "/goods" });
          });
    }
  }
};
</script>

<style lang="scss" scoped>
.el-form {
  width: 400px;
  height: 400px;
}
p {
  margin-top: 40px;
  margin-left: 50px;
}
</style>
